package com.jrx.anytxn.reporting.job.xib.config;

import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.entity.CmInteCoLonRepayDtl;
import com.jrx.anytxn.reporting.job.xib.step.ModifyLonInfoProcessor;
import com.jrx.anytxn.reporting.job.xib.step.ModifyLonInfoWriter;
import com.jrx.anytxn.reporting.job.xib.step.ModifyRepayDtlListener;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * 修改贷款信息已还期数
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class ModifyLonInfoConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(ModifyLonInfoConfiguration.class);

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    private ModifyLonInfoProcessor modifyLonInfoProcessor;
    @Autowired
    private ModifyLonInfoWriter modifyLonInfoWriter;
    @Autowired
    private ModifyRepayDtlListener modifyRepayDtlListener;


    @Bean
    public Job modifyLonInfoJob() {
        return jobBuilderFactory.get("modifyLonInfoJob")
                .incrementer(new RunIdIncrementer())
                .listener(modifyRepayDtlListener)
                .start(modifyLonInfoStep())
                .build();
    }

    @Bean
    public Step modifyLonInfoStep(){
        return stepBuilderFactory.get("modifyLonInfoStep")
                .<CmCustomerFirstLevel, CmInteCoLonRepayDtl>chunk(anyTxnBatchProperties.getReportingCustomerConfig().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(lonInfoFileReader())
                .processor(modifyLonInfoProcessor)
                .writer(modifyLonInfoWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingCustomerConfig().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<CmCustomerFirstLevel> lonInfoFileReader() {
        MyBatisCursorItemReader<CmCustomerFirstLevel> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        cursorItemReader.setQueryId("com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerFirstLevelMapper.selectAllCust");
        SynchronizedItemStreamReader<CmCustomerFirstLevel> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }
}
